﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestSystem.BLL.TestSystem;

namespace TestSystem.BLL.Helpers
{
    public class TestSystemHelper
    {
        public TestSystemHelper()
        {

        }

        public static List<Test> GetVariant(List<Test> allTests, Quiz currentQuiz)
        {
            var parts = currentQuiz.LevelsPercentage.Split('#');
            var levelsCount = parts.Length;
            var levelsCost = new List<int>(levelsCount);
            var questionsPerLevel = new List<int>(levelsCount);
            for (int i = levelsCount - 1; i >= 0; i--)
            {
                levelsCost.Add(int.Parse(parts[i].Split('-')[0]));
                questionsPerLevel.Add(int.Parse(parts[i].Split('-')[1]));
            }

            var result = new List<Test>();
            for (int j = 0; j < levelsCount; j++)
            {
                var query = (from test in allTests
                             where test.Complexity == levelsCost[j]
                             orderby Guid.NewGuid()
                             select test).Take(questionsPerLevel[j]);
                result.AddRange(query.ToList());
            }
            return (from test in result
                    orderby Guid.NewGuid()
                    select test).ToList();
        }
    }
}